package net.wantongtech.xa.uploadbasicinfo.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.clearing.EtcMessageOpt;
import com.wtkj.zwxt.sjhj.constant.BizCodeType;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CheckMissingVersionDao;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;

/**
 * 检查下载缺失版本服务实现
 *
 * @author MaShibo
 * 日期 :  2020/2/10
 */
@Slf4j
@Service
public class CheckMissingVersionService {

    @Resource
    private CheckMissingVersionDao checkMissingVersionDao;

    public void check() {
        DateTimeFormatter minuteFormatter = DateTimeFormat.forPattern("yyyyMMddHHmm");
        LocalDateTime startDateTime = LocalDateTime.parse("202002130000", minuteFormatter);
        LocalDateTime endDateTime = LocalDateTime.parse("202002100000", minuteFormatter);
        LocalDateTime temp = startDateTime;
        for (Integer bizCode : Lists.newArrayList(BizCodeType.ETCTS_FREE_TD_BIZCODE, BizCodeType.ETCTS_RECORD_TD_BIZCODE)) {
            Set<String> missingVersionSet = Sets.newHashSet();
            while (temp.compareTo(endDateTime) >= 0) {
                String version = temp.toString("yyyyMMddHHmm");
                boolean exists = checkMissingVersionDao.existsVersion(version, bizCode);
                if (!exists) {
                    log.info("缺失的版本号：{}", version);
                    missingVersionSet.add(version);
                }
                temp = temp.minusMinutes(5);
            }
            log.info("所有缺失的版本号：{}", missingVersionSet);
            missingVersionSet.forEach(version -> {
                EtcMessageOpt etcMessageOpt = new EtcMessageOpt();
                etcMessageOpt.setListId(new RandomGUID().toString());
                etcMessageOpt.setReqFileName("重新下载");
                etcMessageOpt.setBizCode(bizCode);
                etcMessageOpt.setVersion(version);
                etcMessageOpt.setCreateOn(new Date());
                etcMessageOpt.setNum(0);
                etcMessageOpt.setState(713);
                etcMessageOpt.setIssuerId("610101");
                checkMissingVersionDao.createValueObject(etcMessageOpt);
            });
        }
    }
}
